package test.com.sms.selenium.singlepoint.util;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import test.com.sms.selenium.framework.SeleniumPageUtil;
import test.com.sms.selenium.framework.SpSeleniumTestAbstract;
import test.com.sms.selenium.util.SeleniumUtil;

import com.sms.common.util.FileUtil;

public class SpLoginPageUtil extends SeleniumPageUtil {
	
	private static final Properties environmentProperties = new Properties();
	
	static
	{
		InputStream is = null;
		try
		{
			is = new FileInputStream (FileUtil.getFile("configuration/testenvironment.properties", SpSeleniumTestAbstract.class));
			environmentProperties.load(is);
		}
		catch (Exception e)
		{
			
		}
		finally
		{
			try{is.close();}catch(Exception ex){}
		}
	}
	
	private static final String SP_ROOT_URL = 
			environmentProperties.getProperty("sp.protocol", "http").trim() + "://" + 
			environmentProperties.getProperty("sp.host", "test.mysplogon.com").trim() + ":" + 
			environmentProperties.getProperty("sp.port", "80").trim() + "/" + 
			environmentProperties.getProperty("sp.root.url", "SinglePoint").trim();
	
	private static final String USER_ID = environmentProperties.getProperty("sp.userid", "quwei@beyondsoft.com"); 
	private static final String USER_PASSWORD = environmentProperties.getProperty("sp.password", "T3stingSMS321T");
	
	// WebElement By
	public static final By LOGIN_DIV_WELCOME = By.xpath("//*[@class='blueStrip']");
	public static final By LOGIN_INPUT_USERNAME = By.id("txtUserName");
	public static final By LOGIN_INPUT_PASSWORD = By.id("txtPwd");
	public static final By LOGIN_BUTTON_LOGIN = By.id("btnLogin");
	public static final By LOGIN_CHECKBOX_AUTO_LOGIN = By.id("chkAutoLogin");
	public static final By LOGIN_SPAN_FAIL_MESSAGE = By.id("lblMsg");
	public static final By LOGIN_IMG_LOGIN_GOOGLE = By.id("imgGoogleLogin");
	public static final By LOGIN_LINK_FORGOT_PWD = By.id("btnReset");
	
	// get elements by methods
	public final static WebElement getLoginInputUsername(){
		return SeleniumUtil.getCachedElement(LOGIN_INPUT_USERNAME);
	}
	
	public final static WebElement getLoginInputPassword(){
		return SeleniumUtil.getCachedElement(LOGIN_INPUT_PASSWORD);
	}
	
	public final static WebElement getLoginButtonLogin(){
		return SeleniumUtil.getCachedElement(LOGIN_BUTTON_LOGIN);
	}
	
	public final static WebElement getLoginCheckboxAutoLogin(){
		return SeleniumUtil.getCachedElement(LOGIN_CHECKBOX_AUTO_LOGIN);
	}
	
	/** enter login page*/
	public final static void enter() {
//		SmsSeleniumUtil.get("https://lb.mysplogon.com/SinglePoint/");
		SeleniumUtil.get(SP_ROOT_URL);
		SeleniumUtil.selectDefaultContent();
	}
	
	/** field the user message*/
	public final static void fillUserMessage(){
		SeleniumUtil.sendKeys(getLoginInputUsername(), USER_ID);
		SeleniumUtil.sendKeys(getLoginInputPassword(), USER_PASSWORD);
	}
	
	/** trigger login*/
	public final static void login() {
		enter();
		// login 
		SeleniumUtil.sendKeys(getLoginInputUsername(), USER_ID);
		SeleniumUtil.sendKeys(getLoginInputPassword(), USER_PASSWORD);
//		SeleniumUtil.click(SeleniumUtil.getCachedElement(LOGIN_CHECKBOX_AUTO_LOGIN));
		SeleniumUtil.click(getLoginButtonLogin());
		// goto homepage
		SpHomePageUtil.waitForPageToLoad();
	}
	
	public final static void waitForLoginPageToLoad(){
		SeleniumUtil.waitOnElements(new By[]{
				LOGIN_INPUT_USERNAME, LOGIN_INPUT_PASSWORD, LOGIN_BUTTON_LOGIN, LOGIN_IMG_LOGIN_GOOGLE
		});
		SeleniumUtil.assertTrue(SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(LOGIN_INPUT_USERNAME)));
		SeleniumUtil.assertTrue(SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(LOGIN_INPUT_PASSWORD)));
		SeleniumUtil.assertTrue(SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(LOGIN_BUTTON_LOGIN)));
		SeleniumUtil.assertTrue(SeleniumUtil.isDisplayed(SeleniumUtil.getCachedElement(LOGIN_IMG_LOGIN_GOOGLE)));
	}
}
